﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using IBatisNet.Common.Utilities;
using System.Data;
using ExpertMIS.Entities;

namespace ExpertMIS.Workships
{
    public partial class MyFAQ : ExpertMIS.Codes.PostPage
    {
        private DomainBroker _domainBroker = new DomainBroker();
        private QuestionBroker _questionBroker = new QuestionBroker();
        private PagingSize _pagingSize = new PagingSize();

        private void BuildPagingSize()
        {
            _pagingSize.Limit = 10;

            int pagingStart = 1;
            if (!int.TryParse(Request["start"], out pagingStart) || pagingStart < 1)
            {
                pagingStart = 1;
            }

            _pagingSize.Start = pagingStart;
        }

        private void LoadMyFAQ(string UserOID)
        {
            BuildPagingSize();
            repResult.DataSource = _questionBroker.GetUserPublishedFAQs(UserOID, _pagingSize);
            repResult.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadMyFAQ(CurrentUser.OID);
        }

        #region 分页信息

        protected string GetPriorPageIndex()
        {
            if (_pagingSize.Start - 1 > 0)
                return (_pagingSize.Start - 1).ToString();
            else
                return _pagingSize.Start.ToString();
        }

        protected string GetNextPageIndex()
        {
            if (_pagingSize.Start + 1 <= (int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit))
                return (_pagingSize.Start + 1).ToString();
            else
                return GetLastPageIndex();
        }

        protected string GetLastPageIndex()
        {
            return ((int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit)).ToString();
        }

        protected string GetPageIndex()
        {
            return ((int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit)).ToString();
        }

        protected string GetRecordCount()
        {
            if (_pagingSize.Total < 0) return "0";
            return _pagingSize.Total.ToString();
        }

        #endregion

        protected void repResult_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView row = e.Item.DataItem as DataRowView;
                if (row == null) return;

                #region 问题标题及标题

                Label lblQTitle = e.Item.FindControl("lblQTitle") as Label;

                if (lblQTitle != null)
                {
                    string sDomain = string.Empty;
                    foreach (DomainEntity domain in _domainBroker.GetDomainsByQuerytionOID(row["F_OID"].ToString()))
                    {
                        sDomain = string.Format("<a href='{0}{1}' target='_blank'>[{2}]</a>", ResolveUrl("~/Forums/ExpertList.aspx?oid="), domain.OID, domain.DomainName);
                        break; // TODO, --> 仅仅显示该问题所属行业（领域）的第一个行业的最新问题！
                    }

                    int len = 0;
                    if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue(Request["filter"] + ".len"), out len))
                    {
                        len = 15;
                    }
                    lblQTitle.Text = string.Format("{0} <a href='{1}{2}' target='_black'>{3}</a>", sDomain, ResolveUrl("~/Forums/QuestionForm.aspx?oid="), row["F_OID"], row["F_Title"].ToString());
                }

                #endregion

                #region 发布时间

                Label lblPubDate = e.Item.FindControl("lblPubDate") as Label;

                if (lblPubDate != null)
                {
                    TimeSpan span = DateTime.Now - Convert.ToDateTime(row["F_CreateDate"]);
                    if (span.Days <= 0)
                    {
                        lblPubDate.Text = string.Format("{0}小时", span.Hours);
                    }
                    else
                    {
                        lblPubDate.Text = string.Format("{0}天", span.Days);
                    }
                }
                #endregion
            }
        }
    }
}
